<?php
// +----------------------------------------------------------------------
// | @filename PermissionRepository.php
// +----------------------------------------------------------------------
// | @author 落花轻舞问君愁 <mwcx1990@qq.com>
// +----------------------------------------------------------------------
// | @datetime 2021-10-29 18:25:14
// +----------------------------------------------------------------------
// | @Description
// +----------------------------------------------------------------------

namespace Addons\Backend\Repositories;

use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionRepository extends BaseRepository
{
    protected $model;
    protected $role;

    public function __construct(Permission $permission,Role $role)
    {
        $this->model = $permission;
        $this->role = $role;
    }
    /**
     * 权限列表筛选查询
     * @param $filters
     * @return mixed
     */
    public function queryList($filters)
    {
        return $this->model::query()->when($filters['id'], function ($query, $id) {
            $query->where('id', $id);
        })->when($filters['name'], function ($query, $name) {
            $query->where('name', 'LIKE', "%$name%");
        })->latest()->paginate();
    }

    /**
     * 已分配的权限
     * @param $roleId
     * @return mixed
     */
    public function assignedPermissions($roleId){
        $role = $this->role::query()->findOrFail($roleId);
        return $role->permissions;
    }
}
